<template>
  <up-popup :show="show" mode="left" @close="close" @open="open">
    <view class="user-detail">
      <view class="user-header" @click="editInfoShow = true">
        <image class="user-img" :src="userStore.avatar" />
        <text class="user-name">
          {{ userStore.nickname }}
        </text>
      </view>

      <view class="user-level">
        <view class="level-content" :class="[!isVip && 'user-no-vip']">
          <view class="level-top">
            <text class="level-text">
              {{ isVip ? '会员等级' : '开通会员' }}
            </text>
            <text v-if="isVip" class="level-date">
              有效期：{{ userStore.levelExpireTime }}
            </text>
          </view>
          <view class="level-num">
            {{ isVip ? userStore.courseCount : '??' }}节课程 {{ isVip ? userStore.unitCount : '?' }}项课程
          </view>
        </view>
        <view class="level-info" @click="vipShow = true">
          升级{{ isVip ? '高级' : '' }}会员，可享孩子个性化反馈 〉
        </view>
      </view>

      <view class="user-group">
        <view class="group-title">
          <text class="title-text">
            团队成员
          </text>
          <text class="title-btn" @click="toUserList">
            管理
          </text>
        </view>

        <view class="group-list">
          <template v-for="(item, index) in memberList">
            <view v-if="index < 3" :key="item.memberId" class="list-item">
              <image class="item-img" :src="item.memberAvatar" />
              <text class="item-name">
                {{ item.memberNickname }}
              </text>
            </view>
          </template>
        </view>
      </view>

      <view class="user-course">
        <view class="course-title">
          我的课程
        </view>
        <view class="course-list">
          <view v-for="item in courseList" :key="item.id" class="list-item">
            <text class="item-text">
              {{ item.name }}
            </text>
            <text v-if="item.owned" class="item-text">
              {{ item.courseCount }}
            </text>
            <text v-else class="item-text item-btn" @click="vipShow = true">
              开通 >
            </text>
          </view>
        </view>
      </view>

      <view class="user-logout" @click="logout">
        退出登录
      </view>
    </view>
  </up-popup>
  <vipBox v-model:show="vipShow" @update-data="getData(true)" />
  <editInfo v-model:show="editInfoShow" />
</template>

<script setup lang="ts">
import vipBox from './vipBox.vue';
import editInfo from './editInfo.vue';
import { useUserStore } from '@/store';
import { getMembers, myCourses } from '@/api/user';
import type { CourseInfo, MemberInfo } from '@/api/user/types';

defineOptions({
  options: {
    styleIsolation: 'shared',
  },
});

const props = defineProps({
  show: {
    type: Boolean,
    default: false,
  },
});

const emit = defineEmits(['update:show', 'logout', 'updateData']);
const userStore = useUserStore();
const open = () => {
  emit('update:show', true);
};
const close = () => {
  emit('update:show', false);
};

const isVip = computed(() => {
  return !!userStore.level;
});

const toUserList = () => {
  uni.navigateTo({
    url: '/pages/views/userList/index',
  });
};

const logout = () => {
  // 退出登录
  userStore.logout();
  close();
};

const vipShow = ref(false);
// 编辑头像昵称
const editInfoShow = ref(false);
const courseList = ref<CourseInfo[]>([]);
const memberList = ref<MemberInfo[]>([]);
const getData = (flag: boolean) => {
  // 获取我的阶段列表
  myCourses().then(({ stages }: { stages: CourseInfo[] }) => {
    courseList.value = stages;
  });
  // 获取全部成员列表
  getMembers().then((res: MemberInfo[]) => {
    memberList.value = res;
  });
  if (flag) {
    emit('updateData');
  }
};

watch(() => props.show, (newVal) => {
  if (newVal) {
    getData(false);
  }
});
</script>

<style lang="scss" scoped>
.user-detail {
  position: relative;
  background: #F8F8F8;
  height: 100%;
  padding: 120rpx 40rpx 40rpx 40rpx;
  overflow: auto;

  .user-header {
    display: flex;
    align-items: center;

    .user-img {
      width: 60rpx;
      height: 60rpx;
      border-radius: 50%;
    }

    .user-name {
      padding-left: 16rpx;
      font-weight: bold;
      font-size: 28rpx;
      color: #333333;
    }
  }

  .user-level {
    margin-top: 40rpx;
    background: #fff;
    border-radius: 16rpx;

    .level-content {
      padding: 24rpx;
      background: #54CE89;
      border-radius: 16rpx;
      color: #FFFFFF;

      .level-top {
        display: flex;
        justify-content: space-between;
        align-items: flex-end;

        .level-text {
          font-size: 32rpx;
          line-height: 48rpx;
          font-weight: bold;
        }

        .level-date {
          font-size: 22rpx;
          line-height: 33rpx;
        }
      }

      .level-num {
        padding-top: 40rpx;
        font-size: 26rpx;
        line-height: 40rpx;
      }
    }

    .user-no-vip {
      background: #EDFFF5;
      color: #333;
    }

    .level-info {
      padding: 16rpx 32rpx;
      font-size: 24rpx;
      color: #303030;
      line-height: 36rpx;
    }
  }

  .user-group {
    margin-top: 40rpx;
    padding: 24rpx 32rpx;
    background: #fff;
    border-radius: 16rpx;

    .group-title {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding-bottom: 24rpx;
      border-bottom: 2rpx dashed #DFE1E7;

      .title-text {
        font-weight: 500;
        font-size: 28rpx;
        color: #333333;
        line-height: 42rpx;
      }

      .title-btn {
        font-weight: 500;
        font-size: 28rpx;
        color: #7151ED;
        line-height: 42rpx;
      }
    }

    .group-list {
      padding-top: 12rpx;

      .list-item {
        padding-top: 28rpx;
        display: flex;
        align-items: center;

        .item-img {
          width: 48rpx;
          height: 48rpx;
          border-radius: 50%;
        }

        .item-name {
          padding-left: 12rpx;
          font-weight: 500;
          font-size: 28rpx;
          color: #333333;
          line-height: 42rpx;
        }
      }
    }
  }

  .user-course {
    margin-top: 40rpx;
    padding: 24rpx 32rpx;
    background: #fff;
    border-radius: 16rpx;

    .course-title {
      padding-bottom: 24rpx;
      border-bottom: 2rpx dashed #DFE1E7;

      font-weight: 500;
      font-size: 28rpx;
      color: #333333;
      line-height: 42rpx;
    }

    .course-list {
      padding-top: 12rpx;

      .list-item {
        padding-top: 28rpx;
        display: flex;
        align-items: center;
        justify-content: space-between;

        .item-text {
          font-weight: 500;
          font-size: 28rpx;
          color: #333333;
          line-height: 42rpx;
        }

        .item-btn {
          color: #7151ED;
        }
      }
    }
  }

  .user-logout {
    margin-top: 120rpx;
    font-weight: 500;
    font-size: 28rpx;
    color: rgba(51,51,51,0.7);
    text-align: center;
    line-height: 42rpx;
  }
}
</style>

<style lang="scss">
user-detail {
  .u-popup__content {
    width: 600rpx;
  }
}
</style>
